<?php

namespace app\teacher\controller\learn;

use app\common\controller\Frontend;
use app\common\enums\AccountTypeEnum;
use app\common\enums\LearnMeansEnum;
use app\common\model\banxi\learn\LearnSayQuestionRecord;
use app\common\model\banxi\learn\LearnSkRecord as LearnSkRecordModel;
use app\common\model\banxi\learn\LearnSubjectQuestionRecord;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\student\Student;
use app\common\service\learn\LearnSkRecordService;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/*
 * 上课记录
 * */

class LearnSkRecord extends Frontend
{
    protected array $noNeedLogin = [''];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 分页-销课列表
     * */
    public function listXkPage()
    {
        $limit = $this->request->get('limit', 20);
        $index = $this->request->get('index', 1);
        $searchText = $this->request->get('searchText', '');

        $trainerId = $this->auth->id;
        $LearnSkRecordModel = new LearnSkRecordModel();
        $list = $LearnSkRecordModel->getListXkPage($index, $limit, $trainerId, AccountTypeEnum::TRAINER, $searchText);


        $this->ok('操作成功', result_row($list));
    }

    /*
     * 创建21天抗遗忘
     * */
    public function createTwentyAntiForgetting()
    {
        // 获取请求中的JSON数据
        $data = $this->request->post();

        // 验证必要参数
        $appointmentId = isset($data['appointmentId']) ? $data['appointmentId'] : null;
        $courseId = isset($data['courseId']) ? $data['courseId'] : 0;
        $learnWordList = isset($data['wordList']) ? $data['wordList'] : [];
        $wordRecodIdList = isset($data['wordRecodIdList']) ? $data['wordRecodIdList'] : [];
        $time = isset($data['time']) ? $data['time'] : null;

        if (!$time) {
            $this->error('请选择时间');
        }
        // 确定trainerId和studentId
        $trainerId = $this->auth->id;

        // 构造提交数据
        $submitData = [
            'appointmentId' => $appointmentId,
            'courseId' => $courseId,
            'learnWordList' => $learnWordList,
            'wordRecodIdList' => $wordRecodIdList,
            'time' => $time
        ];

        // 调用业务层方法提交学习记录
        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->createTwentyAntiForgetting(
            $trainerId,
            $submitData
        );
        if (!$result) {
            $this->fail($LearnSkRecordService->getError());
        }
        $this->ok('提交成功', $result);
    }

    /**
     * 获取-上课ID
     */
    public function getSkIdByAppointment()
    {
        // 获取预约ID参数
        $appointmentId = $this->request->get('appointmentId', null);


        // 根据预约ID查询上课记录
        $LearnSkRecordModel = new LearnSkRecordModel();
        $learnSkRecord = $LearnSkRecordModel
            ->where('appointmentId', $appointmentId)
            ->value('id');

        $this->ok('操作成功', $learnSkRecord);

    }

    /*
     * 查询-学习报告
     * */
    public function getLearningReport()
    {
        $skRecordId = $this->request->get('skRecordId');
        if (!$skRecordId) {
            $this->fail('请选择学习记录');
        }
        $LearnSkRecordModel = new LearnSkRecordModel();
        // 获取学习报告数据
        $learningReport = $LearnSkRecordModel->with([
            'studentInfo',
            'accountClassHour'
        ])->find($skRecordId);

        //初始化全科练习数据
        $learningReport['inSubjectQuestion'] = [
            'questionNum' => 0,
            'rightRate' => 0
        ];
        $learningReport['afterSubjectQuestion'] = [
            'questionNum' => 0,
            'rightRate' => 0
        ];
        $LearnSubjectQuestionRecordModel = new LearnSubjectQuestionRecord();
        //全科随堂练习数据
        $in_subject_question_record = $LearnSubjectQuestionRecordModel->where(['skRecordId' => $skRecordId, 'learnMeans' => LearnMeansEnum::E1, 'exerciseType' => 2])->find();
        if ($in_subject_question_record) {
            $learningReport['inSubjectQuestion'] = [
                'questionNum' => $in_subject_question_record['totalNum'],
                'rightRate' => $in_subject_question_record['rightRate']
            ];
        }
        //全科课后作业数据
        $after_subject_question_record = $LearnSubjectQuestionRecordModel->where(['skRecordId' => $skRecordId, 'learnMeans' => LearnMeansEnum::E1, 'exerciseType' => 3])->find();
        if ($after_subject_question_record) {
            $learningReport['afterSubjectQuestion'] = [
                'questionNum' => $after_subject_question_record['totalNum'],
                'rightRate' => $after_subject_question_record['rightRate']
            ];
        }

        $this->ok('操作成功', $learningReport);
    }

    /*
     * 评价
     * */
    public function studentEvaluate()
    {
        $param = $this->request->param(["id" => 0, 'star' => 0, 'content' => '']);
        try {
            $validate = Validate::rule(['id' => ValidateRule::isRequire(null, '请选择你要评价的记录')]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $accountType = $this->auth::TOKEN_TYPE;
            $trainerId = $this->auth->id;
            $LearnSkRecordModel = new LearnSkRecordModel();
            $LearnSkRecordModel->evaluate($trainerId, $accountType, $param['id'], $param['star'], $param['content']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功');

    }


    //学习报告(全部)
    public function getSkRecordReport()
    {
        $skRecordId = $this->request->get('skRecordId');
        if (!$skRecordId) {
            $this->fail('请选择上课记录');
        }
        $LearnSkRecordService = new LearnSkRecordService();
        $result = $LearnSkRecordService->questionReportAll($skRecordId);
        $this->ok('ok', $result);
    }
}